package cn.wsh.user.service.impl;

import cn.wsh.basic.constant.BaseConstant;
import cn.wsh.basic.dto.RegisterDto;
import cn.wsh.basic.exception.BusinessException;
import cn.wsh.basic.util.MD5Utils;
import cn.wsh.basic.util.StrUtils;
import cn.wsh.user.domain.Logininfo;
import cn.wsh.user.domain.User;
import cn.wsh.user.mapper.LogininfoMapper;
import cn.wsh.user.mapper.UserMapper;
import cn.wsh.user.service.IUserService;
import cn.wsh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wushenghua
 * @since 2022-02-14
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {
        //1.参数校验
        if(StringUtils.isEmpty(dto.getPhone())
                || StringUtils.isEmpty(dto.getPassword())
                || StringUtils.isEmpty(dto.getConfirmPassword())
                || StringUtils.isEmpty(dto.getSmsCode())
        ){
            throw new BusinessException("参数有问题！！");
        }
        //2.两次密码是否一直
        if(!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入！！");
        }
        //3.验证码是否有效并且正确
        String smsCodeKey = BaseConstant.SmsConstant.BUSINESS_REGISTER_PREFIX+dto.getPhone();//register:15982485856
        Object smsCodeTmp = redisTemplate.opsForValue().get(smsCodeKey);
        if(smsCodeTmp == null){
            throw new BusinessException("验证码已失效，请重新获取！！");
        }
        String codeTmp = smsCodeTmp.toString().split(":")[0]; //使用历史的验证码
        if(!dto.getSmsCode().equalsIgnoreCase(codeTmp)){
            throw new BusinessException("请填写正确的验证码！！");
        }
        //4.存 t_user -->t_logininfo
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);//返回自增ID
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        //一定不要忘记设置type
        logininfo.setType(1);
        return logininfo;
    }

    /**
     * 初始化user对象
     * @param dto
     * @return
     */
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        //使用Md5 加密 加盐
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }


    /**
     * 我们在操作User表的时候，如果涉及到登录相关信息的修改，需要级联调整logininfo表
     *
     */
    @Override
    public void add(User user) {
        super.add(user);
        //@TODO 保存logininfo
    }

    @Override
    public void update(User user) {
        super.update(user);
        //@TODO 保存logininfo
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        //@TODO 保存logininfo
    }
}
